﻿#include "SystemConfig.h"

#include <QFile>
#include <QCoreApplication>
#include <QFileInfo>
#include <QDir>

#include "common_log.h"

#define HAL_CONFIG_PATH "/config/system.json"
#define SOFT_CONFIG_PATH "/config/business.json"
SystemConfig::SystemConfig()
{}

SystemConfig::~SystemConfig()
{}

static SystemConfig* instance = nullptr;

SystemConfig* SystemConfig::GetInstance()
{
	if (instance == nullptr) {
		instance = new SystemConfig();
	}
	return instance;
}

bool SystemConfig::Init()
{

	return ReadHalCfg() && ReadSoftCfg();
}

QString SystemConfig::GetConfig(ComponentType& type)
{
	switch (type)
	{
	case ComponentType::component_robot:
		return GetRobotConfig();
	default:
		break;
	}

	return "";
}


bool SystemConfig::ReadHalCfg()
{
	QString filename = QCoreApplication::applicationDirPath() + QString(HAL_CONFIG_PATH);
	QFile file(filename);
	if (!file.exists()) {
		return true;
	}
	if (!file.open(QIODevice::ReadOnly)) {
		log_error("file[{}] open failed", filename);
		return false;
	}
	QByteArray Hdata = file.readAll();
	file.close();

	m_hal.fromJson(Hdata);
	return true;
}
bool SystemConfig::ReadSoftCfg()
{
	QString filename = QCoreApplication::applicationDirPath() + QString(SOFT_CONFIG_PATH);
	QFile file(filename);
	if (!file.exists()) {
		return true;
	}
	if (!file.open(QIODevice::ReadOnly)) {
		log_error("file[{}] open failed", filename);
		return false;
	}
	QByteArray data = file.readAll();
	file.close();

	m_soft.fromJson(data);
	return true;
}

bool SystemConfig::SaveHalCfg()
{
	QByteArray data = m_hal.toByteArray(QJsonDocument::Indented);
	QString filename = QCoreApplication::applicationDirPath() + QString(HAL_CONFIG_PATH);
	QFile file(filename);
	if (!file.exists()) {
		QFileInfo f(file.fileName());
		auto path = f.absoluteDir().dirName();
		if (!QDir().mkpath(path)) {
			log_error("Create path[{}] filed", f.absoluteDir().dirName());
			return false;
		}
	}

	if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
		int ret = file.write(data);
		file.close();
		if (ret > 0) {
			return true;
		} else {
			log_error("file write 0 Byte");
			return false;
		}
	}
	log_error("file[{}] open fialed", filename);
	return false;
}

bool SystemConfig::SaveSoftCfg()
{
	QByteArray data = m_hal.toByteArray(QJsonDocument::Indented);
	QString filename = QCoreApplication::applicationDirPath() + QString(HAL_CONFIG_PATH);
	QFile file(filename);
	if (!file.exists()) {
		QFileInfo f(file.fileName());
		auto path = f.absoluteDir().dirName();
		if (!QDir().mkpath(path)) {
			log_error("Create path[{}] filed", f.absoluteDir().dirName());
			return false;
		}
	}

	if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
		int ret = file.write(data);
		file.close();
		if (ret > 0) {
			return true;
		} else {
			log_error("file write 0 Byte");
			return false;
		}
	}
	log_error("file[{}] open fialed", filename);
	return false;
}

QString SystemConfig::GetRobotConfig()
{
	return m_hal.robot();
}
bool SystemConfig::SaveRobotConfig(const QString& cfg)
{
	m_hal.robot() = cfg;
	return SaveHalCfg();
}
